package com.xq.jltx.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.xq.jltx.adapter.SearchNoteAdapter;
import com.xq.jltx.db.NoteDbUtils;
import com.xq.jltx.db.bean.Note;
import com.xq.jltx.greendaopro.R;
import com.xq.jltx.layout.DividerItemDecoration;

import java.util.List;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016/07/21.
 */
public class SearchFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<Note>>{
    private EditText noteEditText;
    private Button searchBtn;
    private RecyclerView noteRecylerView;
    private SearchNoteAdapter searchNoteAdapter;

    public SearchFragment() {
    }

    public static SearchFragment newInstance(){
        SearchFragment searchFragment = new SearchFragment();
        return searchFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.search_fragment_layout,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        assignView(view);
        searchBtn.setOnClickListener(mOnClickListener);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext());
        noteRecylerView.addItemDecoration(dividerItemDecoration);
        // 设置item动画
        noteRecylerView.setItemAnimator(new DefaultItemAnimator());
        noteRecylerView.setItemAnimator(null);
        noteRecylerView.setLayoutManager(linearLayoutManager);
        searchNoteAdapter = new SearchNoteAdapter(getContext(),null,noteRecylerView);
        noteRecylerView.setAdapter(searchNoteAdapter);
        getLoaderManager().initLoader(0, null, this);
    }

    private void assignView(View view){
        noteEditText = (EditText)view.findViewById(R.id.note_edittext);
        searchBtn = (Button)view.findViewById(R.id.search_btn);
        noteRecylerView = (RecyclerView)view.findViewById(R.id.my_note_recylerview);
    }

    @Override
    public Loader<List<Note>> onCreateLoader(int id, Bundle args) {
        String text = "";
        if(args != null && args.getString("TEXT_KEY") != null){
            text = args.getString("TEXT_KEY");
        }
        return new MyLoader(getContext(),text);
    }

    @Override
    public void onLoadFinished(Loader<List<Note>> loader, List<Note> data) {
        searchNoteAdapter.setNoteList(data);
    }

    @Override
    public void onLoaderReset(Loader<List<Note>> loader) {
        searchNoteAdapter.setNoteList(null);
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.search_btn){
                String text = noteEditText.getText().toString().trim();
                Bundle args = new Bundle();
                args.putString("TEXT_KEY",text);
                getLoaderManager().restartLoader(0,args, SearchFragment.this);
            }
        }
    };

    public  static class MyLoader extends AsyncTaskLoader<List<Note>> {
        private String text = "";
        public MyLoader(Context context,String text) {
            super(context);
            this.text = text;
        }

        @Override
        public List<Note> loadInBackground() {
            return NoteDbUtils.getNotesFromTextLike(text);
        }

        @Override
        protected void onStartLoading() {
            forceLoad();
            super.onStartLoading();
        }
    }
}
